<div class="popup_body">
    <div class="ztree" style="height:402px;">
        <div class="ztree_body" id="tree_${pid}">
        </div>
    </div>
    <div class="search">
        <div class="float_right">
            <button class="btn btn_yellow" id="query_${pid}">搜索</button>
        </div>
        <table cellpadding="0" cellspacing="0" border="0">
            <tr>
                <td align="right">&nbsp;商品名称：</td>
                <td><input type="text" class="text" id="item_name_${pid}"/></td>
            </tr>
        </table>
    </div>

    <div class="grid" style="width:600px; height:360px; margin-left:210px;">
        <table id="page_table_${pid}"></table>
    </div>
</div>
<div class="popup_btn">
    <button class="btn btn_red" id="ok_${pid}">确定</button>
    <button class="btn btn_border" id="close_${pid}">取消</button>
</div>
<script>
    (function() {
        var win = $('#${pid}'), windowData = win.data('windowData');
        var tree = $('#tree_${pid}');
        var datagrid = $('#page_table_${pid}');
        tree.tree({
            lines: true,
            onBeforeSelect: App.tree.toggleSelect,
            onClick: function(node) {
                query();
            },
            url: '${contextPath}/security/hdw/item_category/tree.htm?agentId=${Session['SESSION_KEY_USER'].agentId}'
        });

        datagrid.datagrid({
            fit: true,
            width: '100%',
            height: '100%',
            striped: true,
            pagination: true,
            url: "${contextPath}/security/hdw/item/page.htm?agentId=${Session['SESSION_KEY_USER'].agentId}&status=2",
            fitColumns: true,
            pageSize: 10,
            pageList: [10, 50, 100],
            idField: 'id',
            singleSelect: true,
            selectOnCheck: false,
            checkOnSelect: false,
            autoRowHeight: false,
            rowStyler: gridRowStyler,
            columns: [
                [
                    {
                        title: '商品图片',
                        align: 'center',
                        field: 'imagePath',
                        width: 60,
                        formatter: function (val, row) {
                            return "<img style='width:25px; height:25px;' src='${controller.appConfig.staticUrl}"+ val +"' />";
                        }
                    },
                    {
                        title: '商品名称',
                        align: 'center',
                        field: 'itemName',
                        width: 60
                    },
                    {title: '成本价', align: 'center', field: 'costPrice', width: 60},
                    {title: '价格', align: 'center', field: 'price', width: 60},
                    {
                        title: '红包',
                        align: 'center',
                        field: 'enableRedPacket',
                        width: 60,
                        formatter: function(val, row) {
                            if(val == 0) {
                                return '禁用';
                            } else if(val == 1) {
                                return '限制最大金额';
                            } else if(val == 2) {
                                return '限制最大比例';
                            }
                        }
                    },
                    {
                        title: '优惠券',
                        align: 'center',
                        field: 'enableCouponTicket',
                        width: 60,
                        formatter: function(val, row) {
                            if(val == 0) {
                                return '禁用';
                            } else if(val == 1) {
                                return '启用';
                            }
                        }
                    },
                    {
                        title: '状态',
                        align: 'center',
                        field: 'status',
                        width: 60,
                        formatter: function(val, row) {
                            if(val == 1) {
                                return '下架';
                            } else if(val == 2) {
                                return '上架';
                            }
                        }
                    }
                ]
            ],
            onLoadSuccess:function() {
                datagrid.datagrid('clearChecked');
                datagrid.datagrid('clearSelections');
            }
        });
        $('#ok_${pid}').click(function() {
            var item = datagrid.datagrid('getSelected');
            if(item) {
                windowData.ok(item);
                win.window('close');
            } else {
                $.messager.alert('提示信息', '请选择商品');
            }

        });
        $('#close_${pid}').click(function() {
            $('#${pid}').window('close');
        });
        $('#query_${pid}').click(function() {
            query();
        });

        function reload() {
            datagrid.datagrid('reload');
            tree.tree('reload')
        }

        function query() {
            var categoryId = tree.tree('getSelected');
            if(categoryId) {
                categoryId = categoryId.id || '';
            } else {
                categoryId = '';
            }

            datagrid.datagrid('options').queryParams = {
                categoryId: categoryId,
                itemName: $('#item_name_${pid}').val()
            };

            datagrid.datagrid('load');
        }
    })()
</script>